<?php
use OSS\OssClient;
use OSS\Core\OssException;
/**
 * @name IndexController
 * @author Lenovo
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */
class FileController extends Base {

	
	public function uploadAction() {
		$file = $this->getRequest()->getFiles('file');
		$allow_exts=['png','jpg','jpeg'];
		if($file){
			$user = $this->getUser();			
			$ext = pathinfo($file['name']);
			$ext = $ext['extension']??'';
			if(!in_array($ext,$allow_exts)){
				error('只允许上传图片');
			}
			if($file['size'] > 1024*1024){
				error('上传图片不能超过1MB');
			}
			$path = $file['tmp_name'];
			$save_name = date('Ymd',time()) .'/'.uniqid()  .'.'. $ext;
			$config = Yaf_Registry::get('config');
			$oss = $config['oss'];
			$accessKeyId = $oss->OSS_ACCESS_KEY;
            $accessKeySecret = $oss->OSS_SECRET_KEY;
            $endpoint = $oss->OSS_END_POINT;           
            $url = '';
            try{
            	$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
                $response = $ossClient->uploadFile($oss->OSS_BUCKET,$save_name,$path);
                $url = $response->header['oss-request-url'];
            }catch (OssException $e){         
          		 error($e->getMessage());
            } 
            success($url);
		}
	}
}
